Any class that contains an abstract method is automatically abstract
Abstract classes are drawn like regular class in UML, except the name of the class and abstract methods are italicized.
Non-abstract classes are called concrete classes.
Protected Members
Third access specification: protected
May be accessed by methods in a subclass
This unrestricted access makes some tasks easier, but it is better to make all fields private and provide public setters/getters.
Access is somewhere between private and public.
Denoted by # symbol in UML
Default Access Modifier
If you don’t provide an access specifier for a class member, the member is given package access by default, meaning that any method in the same package may access the member.
Denoted by ~
Chains of Inheritance
A superclass can inherit from another class.
Classes are often depicted graphically in a class hierarchy.
The Object Class
All Java classes are directly or indirectly derived from a class named Object in the java.lang package.
Any class that doesn’t specify the extends keyword is automatically derived from the Object class (public class MyClass extends Object).
Ultimately, every class is derived from the Object class.
Thus, every class inherits the Object class’s members (which we often override):
toString(): Returns string containing class name and hash of memory address.
equals(): Accepts address of an object and returns true if it is the same address as the calling object’s address.
Polymorphism
Polymorphism: Ability to take many forms.
In Java, a reference variable is polymorphic because it can reference objects of type different from its own, as long as those types are subclasses of its type.
A superclass reference variable can reference objects of a subclasses.
GradedActivity exam;
We can use the exam variable to reference a GradedActivity object
exam =newGradedActivity();
Or we can create a reference to a FinalExam object
GradedActivity exam =newFinalExam(50,7);
Polymorphism and Dynamic Binding
When a superclass variable references a subclass object that has overridden a method in the superclass, the subclass’s version will be run when it’s called.
Java performs dynamic binding or late binding when a variable contains a polymorphic reference.
The JVM determines at runtime which method to call, using the polymorphic object’s type rather than the reference type.
The instanceof Operator
instanceof can be used to determine whether an object is an instance of a particular class.
FinalExam exam =newFinalExam(20,2);if(exam instanceof GradedActivity)// Does runelse// Doesn't run